package controller;

import dto.UserDto;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhifu on 2015/3/24.
 */
public class LoginOverviewController extends SimpleFormController{

    private UserService userService;

    public LoginOverviewController(){
        setCommandClass(UserDto.class);
        setCommandName("userDto");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        String guid = ServletRequestUtils.getStringParameter(request, "guid", "");
        return userService.loadByGuid(guid);
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        UserDto userDto = (UserDto)command;
        userService.saveOrUpdate(userDto);
        Map model = new HashMap<>();
        model.put("userDto",userDto);
        return new ModelAndView("loginsuccessview",model);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
